using System;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using VSSAHT.Transfer;
using VSS_AHT.DataAccess;
using System.Collections.Generic;
using System.Diagnostics;


namespace VSSAHT.DataAccess
{
	public class ItemDataAccess : ItemDataAccessBase, IItemDataAccess
	{

        private ItemDTO _searchObject;
        /// <summary>
        /// Search Object Property
        /// </summary>
        public ItemDTO SearchObject
        {
            get
            {
                return _searchObject;
            }
            set
            {
                if (_searchObject != value)
                {
                    _searchObject = value;
                }
            }
        }

        #region Methods

        public IList<ItemDTO> Search()
        {
            IList<ItemDTO> collection = null;
            try
            {
                using (VSSAHTDataContext dataContext = new VSSAHTDataContext(base.ConnectionString))
                {
                    IQueryable<Item> query = dataContext.Items;
                    if (!String.IsNullOrEmpty(_searchObject.ItemNumber))
                    {
                        query = query.Where(e => e.ItemNumber.Contains(_searchObject.ItemNumber));
                        //hasConditions = true;
                    }
                    if (!String.IsNullOrEmpty(_searchObject.ItemName))
                    {
                        query = query.Where(e => e.ItemName.Contains(_searchObject.ItemName));
                        //hasConditions = true;
                    }

                    collection = new List<ItemDTO>();
                    foreach (var item in query)
                    {
                        collection.Add(ItemMapper.ToBusinessObject(item));
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return collection;
        }

        //public bool Save(ItemDTO item, ref bool isNew)
        //{
        //    bool result = false;
        //    try
        //    {
        //        using (VSSAHTDataContext dataContext = new VSSAHTDataContext(base.ConnectionString))
        //        {
        //            // Get single entity from database
                   
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        // Update failed
        //        Debug.WriteLine(ex.Message);
        //        throw ex;
        //    }

        //    return result;
        //}
        #endregion

    }
}
